package com.suse.lhw.resourceservere.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.resourceservere.com.suse.lhw.security.controller
 * @ClassName: FileController
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2020/8/5 9:43
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/8/5 9:43
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Controller
public class FileController {
    @RequestMapping("fileUpload")
    public Map<String, Object> fileUpload(@RequestParam MultipartFile multipartFile, HttpServletRequest request) {
        if (multipartFile.isEmpty())
            System.out.println("文件为空");
        String filename = multipartFile.getOriginalFilename();
        //得到文件名字
        String sufficeName = filename.substring(filename.indexOf("."));
        //得到后缀
        String filepath = "D:\\AppFilesWithNoPriority\\毕业设计\\项目\\减肥管理系统\\health_manage_system\\resourceservere\\src\\main\\resources\\static\\image\\userImg";
        //文件上传后的路径
        filename = UUID.randomUUID() + sufficeName;
        //给一个新的名字
        File dest = new File(filepath + filename);
        if (!dest.getParentFile().exists())
            dest.getParentFile().mkdirs();
        try {
            multipartFile.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("filename", filename);
        return map;
    }
}
